/*
* Copyright (c) 2016 . All Rights Reserved.
*/
package com.cgmnx.admin.common.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author: created  Date: 16/12/13 Time: 上午11:25
 * @version: 1.0
 */
public class EncryptionUtil {


    private static final String DEFAULT_SALT_CODE = "mj5XgOwKjUkHlXISnZ7Ynu1HI/w=";

    /**
     * @param userId   用户名
     * @param password 明文密码
     * @return 加密后的密码
     */
    public static String encryptUserInfo(String userId, String password, String salt) {
        return encryptData(userId, password, salt);
    }

    /**
     * @param userId    用户id
     * @param loginTime 用户登陆时间
     * @param salt
     * @return cookie中的验证信息
     */
    public static String encryptCookie(String userId, long loginTime, String salt) {
        if (StringUtils.isBlank(userId) || loginTime <= 0) {
            throw new IllegalArgumentException("negative uid or loginTime");
        }
        return encryptData(userId, String.valueOf(loginTime), salt);
    }

    public static String encryptData(String var1, String var2) {

        return encryptData(var1, var2, DEFAULT_SALT_CODE);
    }

    public static String encryptData(String var1, String var2, String salt) {
        if (StringUtils.isBlank(var1) || StringUtils.isBlank(var2) || StringUtils.isBlank(salt)) {
            throw new IllegalArgumentException("Empty encrypt argument");
        }

        String md5Data = DigestUtils.md5Hex(var1);
        String content = md5Data + var2 + salt;
        return DigestUtils.sha256Hex(content);
    }

}